package com.xiaomi.back.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.back.service.commoditys.CommoditysService;
import com.xiaomi.back.service.describes.DescribesBack;
import com.xiaomi.back.service.malltype.MallService;
import com.xiaomi.back.service.user.UserBackService;
import com.xiaomi.pojo.Commoditys;
import com.xiaomi.pojo.Describes;
import com.xiaomi.pojo.Malltype;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CommoditysController {
    @Autowired
    private CommoditysService commoditysService;
    @Autowired
    private DescribesBack describesBack;
    @Autowired
    private MallService mallService;
    //修改商品上架
    @RequestMapping("/shelves/{cid}")
    public String Shelves(@PathVariable("cid") int cid , Model model) {
        int count=commoditysService.CommoditysUpadte(1,cid);
        model.addAttribute("count",count);
        return "back/Comm/commstate";
    }

    //修改商品下架
    @RequestMapping("/shelf/{cid}")
    public String Shelf(@PathVariable("cid") int cid , Model model) {
        int count=commoditysService.CommoditysUpadte(2,cid);
        model.addAttribute("count",count);
        return "back/Comm/commstate2";
    }

    //按deid 查商品信息
    @RequestMapping("/getDeid/{deid}")
    public String Deidinfo(@PathVariable("deid") Integer deid,Model model){
        Describes describes=describesBack.getDeid(deid);
        model.addAttribute("describes",describes);
        return "back/Comm/Upcount";
    }

    //修改库存
    @PostMapping("/UpCount")
    public String UpCount(int deid, int count, Model model){
        int result=describesBack.UpCount(count,deid);
        return "back/Comm/commlist";
    }

    //查看所有商品
    @RequestMapping("/getComm")
    public String getComm(Model model, @RequestParam(defaultValue = "1") int currentPageNo) {
        PageHelper.startPage(currentPageNo, 7);
        //在售所有商品
        List<Commoditys> commoditys = commoditysService.getCommodityslist();
        PageInfo<Commoditys> page = new PageInfo<>(commoditys);
        model.addAttribute("page", page);

        //查询商品类型
        List<Malltype> malltypes=mallService.getMallType();
        model.addAttribute("malltypes",malltypes);

        return "back/Comm/getComm";
    }


    //查看所有商品信息
    //搜索页
    @GetMapping("/UpConfig")
    public String getShelvesComm(@RequestParam(defaultValue = "1") int currentPageNo, Model model, @RequestParam("name") String comname,@RequestParam(defaultValue = "0") int lx) {
        PageHelper.startPage(currentPageNo, 7);
        List<Commoditys> commoditys = commoditysService.getCommoditysByName(comname,lx);

        PageInfo<Commoditys> page=new PageInfo<>(commoditys);
        model.addAttribute("page", page);
        model.addAttribute("name",comname);
        model.addAttribute("lx",lx);
        return "back/Comm/getCommFind";
    }

    //根据cid 加载修改商品页面
    @RequestMapping("/ClickUp/{cid}")
    public String ClickUp(@PathVariable("cid") Integer cid,Model model){
        Commoditys commoditys=commoditysService.CommoditysById(cid);
        model.addAttribute("commoditys",commoditys);
        return "back/Comm/CommFrom";
    }

    //修改描述
    @PostMapping("/UpDescribe")
    public String UpCount(Integer deid, String desc, Model model){
        int result=commoditysService.UpDesc(desc,deid);
        return "back/Comm/getComm";
    }

    //取消 添加推荐
    @RequestMapping("/addcancel")
    public String AddCancel(@RequestParam("add")int add,@RequestParam("cid")int cid,Model model){
        int result=commoditysService.UpRecommend(add,cid);
        model.addAttribute("result",result);
        return "back/Comm/Result";
    }
}
